<template>
	<a-modal :visible="visible" width="500px" title="作废" centered destroyOnClose @cancel="cancel" :footer="null" :maskClosable="false" :bodyStyle="{padding: 0}">
		<a-form ref="formRef" :model="modelRef" class="flex-container-nomal" @finish="run">
			<div class="flex1 auto-y">
				<a-form-item label="作废原因" name="obsoleteReason" :rules="[{required: true, message: '请输入作废原因'}]">
					<a-textarea v-model:value="modelRef.obsoleteReason" placeholder="请输入作废原因" :rows="4" show-count :maxlength="500" />
				</a-form-item>
			</div>
			<div class="a-modal-dialog-footer">
				<a-space :size="12">
					<a-button @click="cancel">取消</a-button>
					<a-button type="primary" html-type="submit" :loading="loading" :disabled="loading">确定</a-button>
				</a-space>
			</div>
		</a-form>
	</a-modal>
</template>
<script setup lang="ts">
	import {reactive, ref} from 'vue'
	import {useRequest} from 'vue-request'
	import {bookObsolete} from '@/api/contract'
	import {message} from 'ant-design-vue'

	const modelRef = reactive({
		obsoleteReason: ''
	})

	const props = defineProps({
		visible: {
			type: Boolean,
			required: true
		},
		id: {
			type: [String, Number],
			default: ''
		}
	})
	const emit = defineEmits(['update:visible', 'success'])
	const cancel = () => {
		emit('update:visible', false)
		resetForm()
	}

	const sendRequest = () => {
		return bookObsolete({
			id: props.id,
			obsoleteReason: modelRef.obsoleteReason
		})
	}

	const {run, loading} = useRequest(sendRequest, {
		manual: true,
		formatResult: (res: any) => {
			if (res.code == 200) {
				message.success('操作成功')
				let temp = setTimeout(() => {
					emit('success')
					clearTimeout(temp)
				}, 300)
				cancel()
			}
		}
	})
	// 重置表单
	const formRef = ref<any>()
	const resetForm = () => {
		formRef.value.resetFields()
	}
</script>
